package com.grandtech.insurance.common.db.jdbc.pg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class PGTBStructureUtil {
    private PGTBStructureUtil() {

    }

    public static Map readTableStructure(String tableName, List<Map> columns) {
        Map<String, Object> map = new HashMap<>();
        map.put("tableName", tableName);
        map.put("columns", columns);
        Map tableInfo = readLayerInfo(columns);
        map.put("tableInfo", tableInfo);
        return map;
    }


    public static Map readLayerInfo(List<Map> columns) {
        Map<String, Object> map = new HashMap<>();
        String name;
        String type;
        int pk;
        for (int i = 0; i < columns.size(); i++) {
            pk = columns.get(i).get("pk") == null ? 0 : (int) columns.get(i).get("pk");
            name = (String) columns.get(i).get("name");
            type = (String) columns.get(i).get("type");
            if (pk == 1) {
                map.put("pkName", name);
                map.put("pkType", type);
            }
            if (name.toLowerCase().contains("shape") || name.toLowerCase().contains("geom")) {
                map.put("geoColumnName", name);
                map.put("geoColumnType", type);
            }
        }
        return map;
    }
}
